package basic;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public class RotatableSprite extends Sprite{
	private int numFrame;
	private int[] frameSequence;
	private int currentIndex;
	private int numShift;
	public int getCurrentIndex()
	{
		return currentIndex;
	}
	public RotatableSprite(Image img, int w, int h, int n) {
		super(img, w, h);
		numFrame = n;
		currentIndex = 0;
		numShift = 0;
	}
	public void setFrameSequence(int[] sequence)
	{
		frameSequence = sequence;
		currentIndex = 0;
		updateFrame();
		//super.setFrame(frame+numFrame*numShift);
	}
	public void setFrame(int frame)
	{
		//super.setFrame(frame+numFrame*numShift);
		currentIndex = frame;
		updateFrame();
	}
	private void updateFrame()
	{
		int frame = currentIndex;
		if(frameSequence != null)
		{
			frame = frameSequence[currentIndex];
		}
		super.setFrame(frame+numFrame*numShift);
	}
	public void nextFrame()
	{
		currentIndex = (currentIndex+1)%frameSequence.length;
		updateFrame();
	}
	
	public void setRotation(double rotation)
	{
		double r = rotation+15;
		if(r>359)
		{
			r-=360;
		}
		int iR = (int)(r/30);
		int trans; 
		switch(iR){
		case 0:
			numShift = 0;
			trans = TRANS_NONE;
			break;
		case 1:
			numShift = 1;
			trans = TRANS_NONE;
			break;
		case 2:
			numShift = 2;
			trans = TRANS_NONE;
			break;
		case 3:
			numShift = 0;
			trans = TRANS_MIRROR_ROT90;
			break;
		case 4:
			numShift = 2;
			trans = TRANS_MIRROR_ROT180;
			break;
		case 5:
			numShift = 1;
			trans = TRANS_MIRROR_ROT180;
			break;
		case 6:
			numShift = 0;
			trans = TRANS_MIRROR_ROT180;
			break;
		case 7:
			numShift = 2;
			trans = TRANS_MIRROR_ROT270;
			break;
		case 8:
			numShift = 1;
			trans = TRANS_MIRROR_ROT270;
			break;
		case 9:
			numShift = 0;
			trans = TRANS_MIRROR_ROT270;
			break;
		case 10:
			numShift = 2;
			trans = TRANS_MIRROR;
			break;
		case 11:
			numShift = 1;
			trans = TRANS_MIRROR;
			break;
		default:
			trans = TRANS_NONE;
			break;
		}
		setTransform(trans);
		//adjustRotation(trans);
		updateFrame();
	}

}
